package com.PAT.UnSatisfying;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/11/30/22:34
 * @Description:
 * @Target: 差8
 */
public class 快速排序 {
  // 主元小的元素放到它的左边，比主元大的元素放到它的右边
  public static void main(String[] args) {

      Scanner in = new Scanner(System.in);
      int N = in.nextInt();
      int[] arr = new int[N];
      for (int i = 0; i < N; i++) {
          arr[i] = in.nextInt();
      }

      ArrayList list = new ArrayList();
      for (int i = 0; i < arr.length; i++) {
          if (Catch(arr,i,arr[i])){
              list.add(arr[i]);
          }
      }

      System.out.println(list.size());
      for (Object o : list) {
          if (o.equals(list.get(list.size()-1))){
              System.out.print(o);
          }else {
              System.out.print(o+" ");
          }

      }


  }

  // 编写一个,从左边开始找最大,从右边开始找最小,入参位置+数字
    public static boolean Catch(int[] arr,int index,int num){
      boolean flag = false;
      int max = Integer.MIN_VALUE;
      int min = Integer.MAX_VALUE;

        for (int i = 0; i < index; i++) {
            if (arr[i]>max){max=arr[i];}
        }

        for (int i = arr.length-1; i > index; i--) {
            if (arr[i]<min){min=arr[i];}
        }

        if (max<num&&min>num){
            flag = true;
        }

      return flag;
    }

}
